/* Author: Nahuel F. Lema, Maxi Yañez

 */

var compartiTuPuntoController = {
    selects: {
        region: null,
        provincia: null,
        comuna: null,
        init: function () {
            this.region = document.getElementById('selectRegion');
            this.provincia = document.getElementById('selectProvincia');
            this.comuna = document.getElementById('selectComuna');
            this.region.className = 'field span';
            this.provincia.className = 'field span';
            this.comuna.className = 'field span';
            this.disabled('provincia');
            this.disabled('comuna');
            this.bind();
        },
        reload: function () {
            this.region = document.getElementById('selectRegion');
            this.provincia = document.getElementById('selectProvincia');
            this.comuna = document.getElementById('selectComuna');
            this.region.className = 'field span';
            this.provincia.className = 'field span';
            this.comuna.className = 'field span';
            this.bind();
        },
        bind: function () {
            var self = this;
            this.region.onchange = function () {
                var id = this.value;
                var padre = self.provincia.parentNode;
                if (id) {
                    $.ajax({
                        url: scriptsController.defs.baseUrl+'controllers/select/depatamentosByRegion/'+id,
                        type: 'POST',
                        success: function(data) {
                            padre.removeChild(self.provincia);
                            padre.innerHTML += data;
                            self.reload();
                            self.disabled('comuna');
                        }
                    });
                } else {
                    self.disabled('provincia');
                    self.disabled('comuna');
                }
            }
            this.provincia.onchange = function () {
                var id = this.value;
                var padre = self.comuna.parentNode;
                if (id) {
                    $.ajax({
                        url: scriptsController.defs.baseUrl+'controllers/select/comunasByProvincia/'+id,
                        type: 'POST',
                        success: function(data) {
                            padre.removeChild(self.comuna);
                            padre.innerHTML += data;
                            self.reload();
                        }
                    });
                } else {
                    self.disabled('comuna');
                }
            }
        },
        disabled: function (select) {
            this[select].innerHTML = '<option value="">Elegir</option>';
            this[select].disabled = 'disabled';
        }
    },
    
    categoriasToCheck: {
        create:function () {
            var html = '';
            $.ajax({
                url:scriptsController.defs.baseUrl+'controllers/categoriaController/findAll',
                success:function (data) {
                    for (var i in data.categorias) {
                        html += '\
                            <li class="span2">\
                                <label for="'+data.categorias[i].identificador+'-field" class="field-container '+data.categorias[i].identificador+'-field checkbox inline">\
                                    <input type="checkbox" value="'+data.categorias[i].id+'" name="categorias[]" id="'+data.categorias[i].identificador+'-field" class="field"/>\
                                    <b class="title">'+data.categorias[i].nombre+'</b>\
                                </label>\
                            </li>\
                        '
                        $('.category-list').html(html)
                    }
                }
            })
        }
    },

    validate: {

        get: function(){
            var componentsPath = scriptsController.defs.baseUrl + scriptsController.defs.componentsPath + 'jvalidation/',
            self = this;
            $.getScript(componentsPath + 'jquery.validate.min.1.9.js', function(){
                self.bind();
            });
        },

        bind: function(){
            var form = $(js.get.item('#comparti-tu-punto-form')[0]);
            $(js.get.item('#cargar-tu-punto-btn')[0]).click(function(e){
                e.preventDefault();
                form.submit();
            });

            form.validate({
                rules: {
                    'nombre' : {
                        required : true
                    },
                    'email' : {
                        email	 : true
                    },
                    'calle' : {
                        required : true
                    },
                    'numero' : {
                        required : true
                    },
                    'provincia' : {
                        required : true
                    },
                    'departamento' : {
                        required : true
                    },
                    'localidad' : {
                        required : true
                    },
                    'comenetario' : {
                        minlength: 10
                    }
                },
                messages: {
                    'nombre' : {
                        required : "Este campo es requerido"
                    },
                    'email' : {
                        email	 : "Ingrese un email valido"
                    },
                    'calle' : {
                        required : "Este campo es requerido"
                    },
                    'numero' : {
                        required : "Este campo es requerido"
                    },
                    'provincia' : {
                        required : "Este campo es requerido"
                    },
                    'departamento' : {
                        required : "Este campo es requerido"
                    },
                    'provincia' : {
                        required : "Este campo es requerido"
                    },
                    'comentario' : {
                        minlength: "Ingrese un minimo de 10 caracteres"
                    }
                },
                submitHandler: function($form) {
                    scriptsController.fixedLoader.show();
                    $form.submit();
                }
            });
        },

        init: function(){
            this.get();
        }
    },

    init: function(){
        this.categoriasToCheck.create();
        this.selects.init();
        this.validate.init();
    }
}

compartiTuPuntoController.init();